package com.lishe.lishe.lishe.module.main.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.utils.AppUtils;
import com.lishe.lishe.lishe.utils.FileUtils;
import com.lishe.lishe.lishe.utils.PermissionUtils;
import com.lishe.lishe.lishe.widget.NomalProgressBar;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.presenter.FilePresenter;

/**
 * Created by lwa on 2017/9/1.
 */

public class LisheLodingActivity extends BaseActivity {
    private NomalProgressBar loadingbar;
    private boolean isDownLoading = false;

    private long mProgress, mTotal;

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, LisheLodingActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        initView();
        downLoadAPK();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PermissionUtils.request_WRITE_EXTERNAL_STORAGE) {
            boolean success = true;
            for (int i : grantResults) {
                if (i != 0) {
                    success = false;
                    break;
                }
            }
            if (success) {
               if(!isDownLoading) downLoadAPK();
            } else {
                toast(getString(R.string.get_sdcard_permission_fail_please_to_get_permission_for_setting));
                finish();
            }
        }
    }

    private void initView() {
        loadingbar = findViewById(R.id.loading_bar);
        loadingbar.setMaxProgress(100);
    }

    private void downLoadAPK() {
        if (PermissionUtils.isStorageWritePermission(LisheLodingActivity.this)) {
            //下载文件
            FilePresenter.getInstance().downLoad(FileUtils.getAPKExternalFilesDir(LisheLodingActivity.this, Environment.DIRECTORY_DOWNLOADS), "礼舍.apk", new DataResponseListener<String>() {
                @Override
                public void onSuccess(final String fileName) {
                    isDownLoading = false;
                    //运行在子线程
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            loadingbar.setProgress(100);
                            toast(getString(R.string.file_download_success));
                            AppUtils.installApk(LisheLodingActivity.this, fileName);
                        }
                    });

                }

                @Override
                public void onFail(final String failMessage) {
                    isDownLoading = false;
                    //运行在子线程
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toast(R.string.save_file_fail_and_no_sdcard_permission);
                        }
                    });

                }

                @Override
                public void onError(Throwable e) {
                    isDownLoading = false;
                    e.printStackTrace();
                    toast(R.string.network_or_server_exception);
                }

                @Override
                public void onProgress(final long progress, final long total, boolean done) {
                    super.onProgress(progress, total, done);
                    isDownLoading = true;
                    mProgress = progress;
                    mTotal = total;
                    myHandler.sendEmptyMessage(1001);
                }
            });
        }
    }

    int i = 0;
    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e("handleMessage  ", " progress= " + mProgress + ",total = " + mTotal);
            i = (int) (mProgress * 100 / (float) mTotal);
            if (i != 100)
                loadingbar.setProgress(i);
        }
    };
}
